﻿using PeshoMVC.Models;
using PeshoMVC.MyHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace PeshoMVC.Areas.Admin.Controllers
{
    [Authorize(Roles = "admin")]
    public class ManageUsersController : Controller
    {
        public ActionResult Index()
        {
            var collectionModels = Data.GetAllUsers();
            return View(collectionModels);
        }

        public ActionResult Details(Guid id)
        {
            if (id != null)
            {
                var user = Data.GetUser(id);
                return View(user);
            }

            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                // search if this user name or email exist
                var user = Membership.GetUser(model.UserName);
                var userByMail = Membership.GetUserNameByEmail(model.Email);

                if (user == null && userByMail == null)
                {
                    var newUser = Membership.CreateUser(model.UserName, model.Password, model.Email);

                    //Roles.CreateRole("user");
                    Roles.AddUserToRole(newUser.UserName, "user");

                    // write userId in profile table
                    Data.GetId(model.UserName);

                    return RedirectToAction("Index");
                }
                else
                {
                    // see better way
                    ViewBag.Error = "The user name or e-mail already exist!";
                    return View(model);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        public ActionResult Edit(Guid id)
        {
            if (id != null)
            {
                var user = Data.GetUser(id);
                return View(user);
            }
            else
            {
                return View();
            }

        }

        [HttpPost]
        public ActionResult Edit(UserProfileModel model)
        {
            try
            {
                if (model.Day != 0 && model.Mount != 0)
                {
                    model.Birthday = new DateTime(model.Year, model.Mount, model.Day);
                }

                Data.SaveData(model);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(Guid id)
        {
            if (id != null)
            {
                var user = Data.GetUser(id);
                return View(user);
            }

            return View();
        }

        [HttpPost]
        public ActionResult Delete(Guid id, string str)
        {
            try
            {
                var currentUser = Data.GetUser(id);
                var user = Membership.DeleteUser(currentUser.UserName, true);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult GetImage(Guid id)
        {
            var model = Data.GetUser(id);

            return File(model.Picture, "image/jpg");
        }
    }
}
